package com.yyk.yykywd.mvvm.adapter

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yyk.yykywd.*
import com.yyk.yykywd.mvvm.entity.AgentListBean


/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */
class AgentAdapter(private val context: Context,
                   layoutResId: Int,
                   data: List<AgentListBean.Data>) : BaseQuickAdapter<AgentListBean.Data, BaseViewHolder>(layoutResId, data) {
    private lateinit var layout_type: String
    private lateinit var from: String
    private lateinit var customerType: String

    fun setFrom(from: String) {
        this.from = from
    }

    fun setCustomerType(customerType: String) {
        this.customerType = customerType
    }

    fun setLayoutType(layout_type: String) {
        this.layout_type = layout_type
    }

    override fun convert(helper: BaseViewHolder, item: AgentListBean.Data) {
        helper.setText(R.id.tv_agent, item.agentname)
        helper.setText(R.id.tv_agent_contact, item.phone)
        helper.setText(R.id.tv_region, item.area)
        helper.setText(R.id.tv_hospital, item.hos_name)
        helper.setText(R.id.tv_hospital_type, item.type_name)
        helper.setText(R.id.tv_visitor, item.call_name)
        helper.setText(R.id.tv_contact, item.call_phone)
        helper.setText(R.id.tv_number, item.order_code)
        helper.setText(R.id.tv_site_type, item.status)
        item.already_money?.let { helper.setText(R.id.tv_amount_received, it) }
        helper.setText(R.id.tv_contract_agent, item.agentname)
        helper.setText(R.id.tv_contract_hos, item.hos_name)
        val tvStatus: TextView = helper.getView(R.id.tv_status)
        val tvIntoHighSea: TextView = helper.getView(R.id.tv_into_high_sea)
        val tvSiteNumHint: TextView = helper.getView(R.id.tv_site_num_hint)
        val tvSiteNum: TextView = helper.getView(R.id.tv_site_num)
        val tvType: TextView = helper.getView(R.id.tv_type)

        if (item.belong_type == 1) {
            helper.setText(R.id.tv_founder, "创建人：" + item.addname)
            helper.setText(R.id.tv_create_time, "创建时间：" + getCommonTime(item.create_time))
        } else {
            helper.setText(R.id.tv_founder, "认领人：" + item.meetname)
            helper.setText(R.id.tv_create_time, "认领时间：" + getCommonTime(item.update_time))
        }

        val clAgent: ConstraintLayout = helper.getView(R.id.cl_agent)
        val clHospital: ConstraintLayout = helper.getView(R.id.cl_hospital)
        val clContract: ConstraintLayout = helper.getView(R.id.cl_contract)
        val tvAgentContactHint: TextView = helper.getView(R.id.tv_agent_contact_hint)
        val tvAgentContact: TextView = helper.getView(R.id.tv_agent_contact)
        val tvVisitorHint: TextView = helper.getView(R.id.tv_visitor_hint)
        val tvVisitor: TextView = helper.getView(R.id.tv_visitor)
        val tvContactHint: TextView = helper.getView(R.id.tv_contact_hint)
        val tvContact: TextView = helper.getView(R.id.tv_contact)


        val tvAmountReceivedHint: TextView = helper.getView(R.id.tv_amount_received_hint)
        val tvAmountReceived: TextView = helper.getView(R.id.tv_amount_received)
        val tvContractAgentHint: TextView = helper.getView(R.id.tv_contract_agent_hint)
        val tvContractAgent: TextView = helper.getView(R.id.tv_contract_agent)
        val tvContractHosHint: TextView = helper.getView(R.id.tv_contract_hos_hint)
        val tvContractHos: TextView = helper.getView(R.id.tv_contract_hos)


        tvStatus.text = item.type
        if (layout_type == AGENT_LAYOUT) {
            clAgent.visibility = View.VISIBLE
            clHospital.visibility = View.GONE
            clContract.visibility = View.GONE
            tvType.setBackgroundResource(R.color.col_f5c161)

            when (customerType) {
                "1" -> {
                    tvIntoHighSea.visibility = View.VISIBLE
                    if (item.add_gh_day == 0) {
                        tvIntoHighSea.text = "于今晚24:00流至公海"
                    } else {
                        tvIntoHighSea.text = item.add_gh_day.toString() + "天后流至公海"
                    }
                    tvSiteNumHint.visibility = View.GONE
                    tvSiteNum.visibility = View.GONE
                    tvStatus.setBackgroundResource(R.drawable.shape_2dp2)
                    tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_e61a13))
                }
                "2" -> {
                    tvIntoHighSea.visibility = View.VISIBLE
                    if (item.add_gh_day == 0) {
                        tvIntoHighSea.text = "于今晚24:00流至公海"
                    } else {
                        tvIntoHighSea.text = item.add_gh_day.toString() + "天后流至公海"
                    }
                    tvSiteNumHint.visibility = View.GONE
                    tvSiteNum.visibility = View.GONE
                    tvStatus.setBackgroundResource(R.drawable.shape_2dp4)
                    tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_5987ff))
                }
                "3" -> {
                    tvIntoHighSea.visibility = View.GONE
                    tvSiteNumHint.visibility = View.VISIBLE
                    tvSiteNum.visibility = View.VISIBLE
                    tvSiteNum.text = item.agency_count.toString()
                    tvStatus.setBackgroundResource(R.drawable.shape_2dp2)
                    tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_e61a13))
                }
                "4" -> {
                    tvIntoHighSea.visibility = View.GONE
                    tvSiteNumHint.visibility = View.VISIBLE
                    tvSiteNum.visibility = View.VISIBLE
                    tvSiteNum.text = item.agency_count.toString()
                    tvStatus.setBackgroundResource(R.drawable.shape_2dp3)
                    tvStatus.setTextColor(ContextCompat.getColor(context, R.color.gray_aa))
                }
            }
        } else if (layout_type == HOSPITAL_LAYOUT) {
            clAgent.visibility = View.GONE
            clHospital.visibility = View.VISIBLE
            clContract.visibility = View.GONE
            tvType.setBackgroundResource(R.color.col_68c3d4)

            if (customerType == "1") {
                tvIntoHighSea.visibility = View.VISIBLE
                if (item.add_gh_day == 0) {
                    tvIntoHighSea.text = "于今晚24:00流至公海"
                } else {
                    tvIntoHighSea.text = item.add_gh_day.toString() + "天后流至公海"
                }
                tvStatus.setBackgroundResource(R.drawable.shape_2dp2)
                tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_e61a13))
            } else if (customerType == "2") {
                tvIntoHighSea.visibility = View.VISIBLE
                if (item.add_gh_day == 0) {
                    tvIntoHighSea.text = "于今晚24:00流至公海"
                } else {
                    tvIntoHighSea.text = item.add_gh_day.toString() + "天后流至公海"
                }
                tvStatus.setBackgroundResource(R.drawable.shape_2dp4)
                tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_5987ff))
            } else if (customerType == "3") {
                tvIntoHighSea.visibility = View.GONE
                tvStatus.setBackgroundResource(R.drawable.shape_2dp2)
                tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_e61a13))
            }
        } else if (layout_type == CONTRACT_LAYOUT) {
            tvIntoHighSea.visibility = View.GONE
            clAgent.visibility = View.GONE
            clHospital.visibility = View.GONE
            clContract.visibility = View.VISIBLE
            tvAmountReceivedHint.visibility = View.GONE
            tvAmountReceived.visibility = View.GONE
            tvContractAgentHint.visibility = View.GONE
            tvContractAgent.visibility = View.GONE
            tvContractHosHint.visibility = View.VISIBLE
            tvContractHos.visibility = View.VISIBLE
            tvStatus.setBackgroundResource(R.drawable.shape_2dp3)
            tvStatus.setTextColor(ContextCompat.getColor(context, R.color.gray_aa))
            tvType.setBackgroundResource(R.color.col_68c3d4)
        } else {
            tvStatus.text = "公海客户"
            tvIntoHighSea.visibility = View.GONE
            tvStatus.setBackgroundResource(R.drawable.shape_2dp4)
            tvStatus.setTextColor(ContextCompat.getColor(context, R.color.col_5987ff))
            if (item.client_type == 1) {
                clAgent.visibility = View.GONE
                clHospital.visibility = View.VISIBLE
                clContract.visibility = View.GONE

                tvVisitorHint.visibility = View.GONE
                tvVisitor.visibility = View.GONE
                tvContactHint.visibility = View.GONE
                tvContact.visibility = View.GONE
            } else {
                clAgent.visibility = View.VISIBLE
                clHospital.visibility = View.GONE
                clContract.visibility = View.GONE

                tvAgentContactHint.visibility = View.GONE
                tvAgentContact.visibility = View.GONE
                helper.setText(R.id.tv_agent, item.hos_name)
                helper.setText(R.id.tv_region, item.type_name)
            }

            helper.setText(R.id.tv_founder, "创建人：" + item.name)
            helper.setText(R.id.tv_create_time, "创建时间：" + getCommonTime(item.create_time))
            if (item.client_type == 1) {
                tvType.setBackgroundResource(R.color.col_68c3d4)
            } else {
                tvType.setBackgroundResource(R.color.col_f5c161)
            }
        }
    }
}